package ycl.common.config;

import cn.hutool.core.util.StrUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import ycl.common.properties.RedisProperties;

import javax.annotation.Resource;
import java.time.Duration;

/**
 * redis配置
 *
 * @author ycl
 * @date 2022/6/17 0017 16:11:36
 */
@Configuration
public class RedisConfig {

    @Resource
    private RedisProperties redisProperties;


    @Primary
    @Bean(name = {"stringRedisTemplate", "stringRedisTemplate0"})
    public StringRedisTemplate stringRedisTemplate0() {
        return stringRedisTemplate(RedisProperties.DB.db0);
    }


    @Bean(name = "stringRedisTemplate1")
    public StringRedisTemplate stringRedisTemplate1() {
        return stringRedisTemplate(RedisProperties.DB.db1);
    }

    @Bean(name = "stringRedisTemplate2")
    public StringRedisTemplate stringRedisTemplate2() {
        return stringRedisTemplate(RedisProperties.DB.db2);
    }

    @Bean(name = "stringRedisTemplate3")
    public StringRedisTemplate stringRedisTemplate3() {
        return stringRedisTemplate(RedisProperties.DB.db3);
    }

    @Bean(name = "stringRedisTemplate4")
    public StringRedisTemplate stringRedisTemplate4() {
        return stringRedisTemplate(RedisProperties.DB.db4);
    }

    @Bean(name = "stringRedisTemplate5")
    public StringRedisTemplate stringRedisTemplate5() {
        return stringRedisTemplate(RedisProperties.DB.db5);
    }

    @Bean(name = "stringRedisTemplate6")
    public StringRedisTemplate stringRedisTemplate6() {
        return stringRedisTemplate(RedisProperties.DB.db6);
    }

    @Bean(name = "stringRedisTemplate7")
    public StringRedisTemplate stringRedisTemplate7() {
        return stringRedisTemplate(RedisProperties.DB.db7);
    }

    @Bean(name = "stringRedisTemplate8")
    public StringRedisTemplate stringRedisTemplate8() {
        return stringRedisTemplate(RedisProperties.DB.db8);
    }

    @Bean(name = "stringRedisTemplate9")
    public StringRedisTemplate stringRedisTemplate9() {
        return stringRedisTemplate(RedisProperties.DB.db9);
    }

    @Bean(name = "stringRedisTemplate10")
    public StringRedisTemplate stringRedisTemplate10() {
        return stringRedisTemplate(RedisProperties.DB.db10);
    }

    @Bean(name = "stringRedisTemplate11")
    public StringRedisTemplate stringRedisTemplate11() {
        return stringRedisTemplate(RedisProperties.DB.db11);
    }

    @Bean(name = "stringRedisTemplate12")
    public StringRedisTemplate stringRedisTemplate12() {
        return stringRedisTemplate(RedisProperties.DB.db12);
    }

    @Bean(name = "stringRedisTemplate13")
    public StringRedisTemplate stringRedisTemplate13() {
        return stringRedisTemplate(RedisProperties.DB.db3);
    }

    @Bean(name = "stringRedisTemplate14")
    public StringRedisTemplate stringRedisTemplate14() {
        return stringRedisTemplate(RedisProperties.DB.db14);
    }

    @Bean(name = "stringRedisTemplate15")
    public StringRedisTemplate stringRedisTemplate15() {
        return stringRedisTemplate(RedisProperties.DB.db15);
    }

    /**
     * 重构string redis template
     *
     */
    private StringRedisTemplate stringRedisTemplate(RedisProperties.DB db) {
        if (db == null || !db.isEnable())
            return null;
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(redisProperties.getHost());//设计ip
        configuration.setPort(redisProperties.getPost());//设置端口
        String password = redisProperties.getPassword();
        if (StrUtil.isNotBlank(password))
            configuration.setPassword(password);
        LettuceClientConfiguration client = LettuceClientConfiguration.builder()
                .commandTimeout(Duration.ofSeconds(redisProperties.getTimeout()))
                .build();
        //通过工厂类获取配置对象
        LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration, client);
        factory.setDatabase(db.getNum());
        factory.afterPropertiesSet();
        return new StringRedisTemplate(factory);
    }
}
